package ru.stepuk.modeller.buffers;

import java.util.ArrayList;
import ru.stepuk.modeller.Request;
import ru.stepuk.modeller.generators.RandomNumberGenerator;

/**
 *
 * @author ivan
 */
public class RandomAccessQueue extends Buffer {

    private RandomNumberGenerator rng;

    public RandomAccessQueue(int capacity, RandomNumberGenerator rng) {
        this.capacity = capacity;
        this.rng = rng;
        container = new ArrayList<Request>(capacity);
    }

    public RandomAccessQueue(RandomNumberGenerator rng) {
        this.rng = rng;
        container = new ArrayList<Request>();
    }

    @Override
    public void putRequest(Request request) throws FullQueueException {
        if (!hasFreeSpace()) {
            throw new FullQueueException();
        }
        container.add(request);
    }

    @Override
    public Request getRequest() {
        return container.get((int) Math.round(rng.nextValue() * getSize()));
    }

}
